using UnityEngine;
using UnityEngine.UI;

public class OpenSettingPanel : MonoBehaviour
{
    public Slider delaySetting;
    public Text delayText;
    public AudioClip[] sound;
    public AudioSource AS;

    float posX, posY;
    bool needAnimation = false, isToShow = false;
    float runTime = 0;
    void Start()
    {
        this.GetComponent<CanvasGroup>().alpha = 0f;
        posX = this.GetComponent<RectTransform>().position.x;
        posY = this.GetComponent<RectTransform>().position.y;
        this.GetComponent<RectTransform>().position = new Vector3(posX - 200, posY, 0);
        PlaySound.delayTime = 0.1f;
    }

    public void ShowSettingPanel()
    {
        isToShow = true;
        needAnimation = true;
        runTime = 0;
        this.GetComponent<Image>().color = Color.HSVToRGB(Random.Range(0, 361) / 360f, 0.4f, 0.5f);
        this.gameObject.SetActive(true);
        AS.PlayOneShot(sound[0]);
    }

    public void HideSettingPanel()
    {
        isToShow = false;
        needAnimation = true;
        runTime = 0;
        AS.PlayOneShot(sound[1]);
    }

    public void PlayBackSound()
    {
        AS.PlayOneShot(sound[1]);
    }

    public void setDelay()
    {
        float input = delaySetting.value;
        input *= 0.05f;
        PlaySound.delayTime = input;
        delayText.text = input + "s";
    }

    // Update is called once per frame
    void Update()
    {
        if(needAnimation)
        {
            if (isToShow)
            {
                runTime += Time.deltaTime;
                this.GetComponent<RectTransform>().position = new Vector3(posX - 1600 * (0.25f - runTime) * (0.25f - runTime), posY, 0);
                this.GetComponent<CanvasGroup>().alpha = runTime * 4;
                if (runTime >= 0.25f)
                {
                    this.GetComponent<RectTransform>().position = new Vector3(posX, posY, 0);
                    this.GetComponent<CanvasGroup>().alpha = 1;
                    needAnimation = false;
                }
            }
            else
            {
                runTime += Time.deltaTime;
                this.GetComponent<RectTransform>().position = new Vector3(posX - 800 * runTime * runTime, posY, 0);
                this.GetComponent<CanvasGroup>().alpha = 1 - runTime * 4;
                if (runTime >= 0.25f)
                {
                    this.GetComponent<RectTransform>().position = new Vector3(posX - 200, posY, 0);
                    this.GetComponent<CanvasGroup>().alpha = 0;
                    needAnimation = false;
                    this.gameObject.SetActive(false);
                }
            }
        }
    }
}
